package com.app.outdoor.adapter;

import java.util.ArrayList;

import com.app.Activity.CommonUtilities;
import com.app.Activity.R;
import com.app.outdoor.pojo.TinNhanPOJO;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class TinNhanAdapter extends BaseAdapter {
	public static class ViewHolder {
		TextView txtTen, txtTinNhan, txtThoiGian;
		Button btnTraLoi;
	}

	ArrayList<TinNhanPOJO> data;
	Context context;
	private LayoutInflater inflater;

	public TinNhanAdapter(Context context, ArrayList<TinNhanPOJO> data) {
		this.context = context;
		this.data = data;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int index) {
		return data.get(index);
	}

	@Override
	public long getItemId(int position) {
		return data.get(position).getId();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			view = inflater.inflate(R.layout.od_row_message, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.txtTen = (TextView) view.findViewById(R.id.txtTen);
			viewHolder.txtTinNhan = (TextView) view.findViewById(R.id.txtTinNhan);
			viewHolder.btnTraLoi=(Button)view.findViewById(R.id.btnTraLoi);
			view.setTag(viewHolder);
		}

		ViewHolder holder = (ViewHolder) view.getTag();
		holder.txtTen.setText(CommonUtilities.contacts.getNguoiDungByID(data.get(position).getIdNguoiGui())
				.getTenDangNhap()+": ");
		holder.txtTinNhan.setText(data.get(position).getNoiDung());
		holder.btnTraLoi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				TinNhanPOJO tinNhan=data.get(position);
					if(tinNhan.getLoai()==1){
					AlertDialog.Builder builder=new AlertDialog.Builder(context);
					builder.setTitle(CommonUtilities.contacts.getNguoiDungByID(tinNhan.getIdNguoiGui()).getTenDangNhap());
					builder.setMessage("Mời bạn vào nhóm: "+tinNhan.getNoiDung());
					builder.setPositiveButton("Đồng ý", new DialogInterface.OnClickListener() {					
						@Override
						public void onClick(DialogInterface dialog, int which) {
							
						}
					});
					builder.setNegativeButton("Từ chối", null);
					
					builder.create().show();
				}
			}
		});
		return view;

	}

}
